package Sort3;

public class TwoSort1 {
    //二分法排序
    public int search (int[] nums, int target) {
        // write code here
        if(nums==null){
            return -1;
        }
        int l=0;
        int r=nums.length-1;
        while(l<=r){
            int mid=(l+r)/2;
            if(nums[mid]>target){
                r=mid-1;
            }else if(nums[mid]<target){
                l=mid+1;
            }else{
                return mid;
            }
        }
        return -1;
    }
    //利用二分法在二维数组里查找目标值
    public boolean Find (int target, int[][] array) {
        // write code here
        if(array.length==0){
            return false;
        }
        if(array[0].length==0){
            return false;
        }
        int m=array.length;
        int n=array[0].length;
        for(int i=0;i<m;i++){
            int l=0;
            int r=array[i].length-1;
            while(l<=r){
                int mid=(l+r)/2;
                if(array[i][mid]<target){
                    l=mid+1;
                }else if(array[i][mid]>target){
                    r=mid-1;
                }else{
                    return true;
                }
            }
        }
        return false;
    }
}
